package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.param.StoreStatusUpdateParam;
import com.tarena.lbs.business.pojo.param.UserLocationParam;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.service.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 店铺管理控制器
 * 适配网关路由：/admin/basic/store/** -> lbs-business服务
 */
@Tag(name = "店铺相关功能接口", description = "包含店铺CRUD、多区域查询、定位上报等功能")
@RestController
@Slf4j
public class StoreController {

    @Resource
    private StoreService storeService;

    // -------------------------- 基础门店管理接口 --------------------------

    /**
     * 分页查询门店列表
     * 路径：/admin/basic/store/list -> 匹配网关路由
     */
    @Operation(summary = "分页查询门店列表", description = "支持多条件筛选的通用列表接口")
    @PostMapping("/admin/basic/business/store/list")
    public Result<PageResult<StoreVO>> pageList(@RequestBody StoreQuery query) {
        log.debug("分页查询门店列表参数: {}--->", query);
        System.out.println("分页查询门店列表参数: {}--->"+ query);
        PageResult<StoreVO> pageResult = storeService.pageList(query);
        return new Result(pageResult);
    }

    /**
     * 新增门店
     * 路径：/admin/basic/store/add -> 匹配网关路由
     */
    @Operation(summary = "新增门店", description = "基础门店信息新增接口")
    @PostMapping("/admin/basic/business/store/add")
    public Result<Void> addStore(@RequestBody StoreParam param) {
        System.out.println("param::"+param);
        boolean success = storeService.saveStore(param);
        return success ? Result.success() : Result.fail("1","新增失败");
    }

    /**
     * 删除门店
     * 路径：/admin/basic/store/delete -> 匹配网关路由
     */
    @Operation(summary = "删除门店", description = "根据ID删除门店信息")
    @PostMapping("/admin/basic/business/store/delete")
    public Result<Void> deleteStore(@RequestBody Map<String, Integer> request ) {//@RequestBody
        Integer id = request.get("id");
        boolean success = storeService.removeStore(id);
        return success ? Result.success() : Result.fail("-1","删除失败");
    }

    /**
     * 更新门店状态
     * 路径：/admin/basic/store/updateStatus -> 匹配网关路由
     */
    @Operation(summary = "更新门店状态", description = "修改门店启用/禁用状态")
    @PostMapping("/admin/basic/business/store/updateStatus")
    public Result<Void> updateStatus(@RequestBody StoreStatusUpdateParam  param) {

        // 添加调试信息
        log.info("更新门店状态参数: {}", param);

        Integer id = param.getId();
        Integer status = param.getStatus();
        
        if (id == null || status == null) {
            log.warn("ID或状态为空，无法更新门店状态");
            return Result.fail("-2", "参数不能为空");
        }
        
        boolean success = storeService.updateStatus(id, status);
        log.info("更新门店状态结果: {}", success);
        
        return success ? Result.success() : Result.fail("-1", "状态更新失败");
    }

    // -------------------------- 后台管理专用接口 --------------------------

    /**
     * 后台查询店铺列表（带权限过滤）
     * 路径：/admin/basic/store/getAll -> 匹配网关路由
     */
//    @Operation(summary = "后台查询店铺列表", description = "管理员视角的店铺列表查询，包含权限过滤")
//    @PostMapping("/admin/basic/store/getAll")
//    public Result<PageResult<StoreVO>> adminPageList(@RequestBody StoreQuery query, Principal principal) throws BusinessException {
//        // 实际场景：从登录用户信息中获取商家ID，过滤数据
//        // 示例：query.setBusinessId(getCurrentUserBusinessId(principal));
//        return new Result(storeService.pageList(query));
//    }


    /**
     * 按多城市ID查询店铺（关联登录用户所属商家）
     * 路径：/admin/basic/store/getStoreByCity -> 匹配网关路由
     */
//    @Operation(summary = "按多城市查询店铺列表", description = "查询当前用户所属商家及指定多个城市的店铺")
//    @PostMapping("/admin/basic/store/getStoreByCity")
//    public Result<PageResult<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery query, Principal principal) throws BusinessException {
//        // 实际场景：从登录用户信息中获取所属商家ID，设置到查询参数
//        // 示例：query.setBusinessId(getCurrentUserBusinessId(principal));
//
//        // 校验城市ID列表非空
//        if (query.getCityIdList() == null || query.getCityIdList().isEmpty()) {
//            throw new BusinessException("1","请至少选择一个城市");
//        }
//
//        PageResult<StoreVO> pageResult = storeService.getStoreByCityIds(query);
//        return new Result(pageResult);
//    }

    // -------------------------- 移动端接口 --------------------------

    /**
     * 手机小程序定位上报
     * 路径：/basic/store/location -> 网关默认转发
     */
    @Operation(summary = "手机小程序定位上报", description = "用户位置信息实时上报接口")
    @PostMapping("/basic/store/location")
    public Result<Void> location(@RequestBody UserLocationParam param) throws BusinessException {
        // 实际实现：验证用户身份 -> 存储经纬度和时间戳
        return Result.success();
    }
}